﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVCClubSite.IDAL;
using MVCClubSite.Model;

namespace MVCClubSite.BLL
{
    /// <summary>
    /// This is the business component to manage announcements
    /// </summary>
    public class AnnouncementsService
    {
        IAnnouncements _announcements;

        /// <summary>
        /// Default constructor that will use the DAL set in the web config
        /// </summary>
        public AnnouncementsService()
        {
            _announcements = MVCClubSite.DALFactory.DataAccess.CreateAnnouncements();
        }

        /// <summary>
        /// Constructor for testing with a different DAL
        /// </summary>
        /// <param name="announcements">DAL for the announcement service to use</param>
        public AnnouncementsService(IAnnouncements announcements)
        {
            _announcements = announcements;
            if (_announcements == null)
                throw new InvalidOperationException("Announcements DAL cannot be null.");
        }

        /// <summary>
        /// Method to get all announcements
        /// </summary>
        /// <returns>List of Announcements</returns>
        public List<Announcement> GetallAnnouncements()
        {
            return (from a in _announcements.GetAllAnnouncements()
                    orderby a.AnnouncementDate descending, a.ID descending 
                    select a).ToList();
        }

        /// <summary>
        /// Method to get all visible announcements
        /// </summary>
        /// <returns>List of Announcements</returns>
        public List<Announcement> GetVisibleAnnouncements()
        {
            return (from a in _announcements.GetVisibleAnnouncements()
                    orderby a.AnnouncementDate descending, a.ID descending 
                    select a).ToList();
        }

        /// <summary>
        /// Method to get the most recent announcements
        /// </summary>
        /// <param name="count">Number of announcements to return</param>
        /// <returns>List of Announcements</returns>
        public List<Announcement> GetRecentAnnouncements(int count)
        {
            return (from a in _announcements.GetRecentAnnouncements(count)
                    orderby a.AnnouncementDate descending, a.ID descending 
                    select a).ToList();
        }

        /// <summary>
        /// Method to get an announcement by the announcement id
        /// </summary>
        /// <param name="id">ID of the announcement to return</param>
        /// <returns>An announcement business entitiy</returns>
        public Announcement GetAnnouncementByID(int id)
        {
            return _announcements.GetAnnouncementByID(id);
        }

        /// <summary>
        /// Method to insert or update an announcement
        /// </summary>
        /// <param name="announcement">announcement to insert or update</param>
        public void Save(Announcement announcement)
        {
            if (announcement == null)
                throw new InvalidOperationException("Announcement cannot be null.");

            if (announcement.AnnouncementDate == null)
                announcement.AnnouncementDate = DateTime.Now;

            _announcements.Save(announcement);
        }

        /// <summary>
        /// Method to delete an announcement
        /// </summary>
        /// <param name="id">ID of the announcement to delete</param>
        public void Delete(int id)
        {
            _announcements.Delete(id);
        }
    }
}
